<?php
// @formatter:off
/**
 * @file c.edit.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-27 04:56:35 +0000 (Mon, 27 May 2013) $
 *
 * $Id: c.edit.php 67 2013-05-27 04:56:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/extensions/TagsRelator/controlers/c.edit.php $
 */
// @formatter:on

global $wcSession;
global $wcRule;
/**
 * @var TagsRelator
 */
global $wcRuleAction;

if($wcRule && $wcRuleAction && $wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
	$mode = $wcRuleAction->mode;

	if($_SERVER["REQUEST_METHOD"] == "POST") {
		global $wcDatabaseAccess;

		$wcRule->indexed(false);

		if($_POST["EDITACTION"] == "ADD" && $_POST["ACTION_NAME"] != $wcRuleAction->name()) {
			$wcRuleAction->setName($_POST["ACTION_NAME"]);
			$wcThemeAssigns["RULE-ACTION-NAME"] = $wcRuleAction->name();
		}

		$db = WCConnection::Instance();
		$dbprefix = $wcDatabaseAccess['prefix'];

		$auxDir = dirname(__FILE__);
		$modeManager = "{$auxDir}/c.edit-{$_POST["MODE"]}.php";
		if(is_readable($modeManager)) {
			require_once $modeManager;
		}
	}

	$wcThemeAssigns["PATH"] = "Action Edit: '{$wcRuleAction->name()}' of rule '{$wcRule->name()}'";
	$wcThemeAssigns["TARE-URI"] = WC_E_TAGS_RELATOR_URI;
	$wcThemeAssigns["mode-template"] = WC_E_TAGS_RELATOR_TEMPLATES."/c.edit-{$mode}.html";
	$wcThemeAssigns["form-action"] = requestParams(true, $wcAction, array(), false);

	switch($mode) {
		case "POOL":
			$wcThemeAssigns["mode"] = $mode;
			$wcThemeAssigns["mode-description"] = $mode;

			$tags = array();
			foreach($wcRuleAction->tags() as $tag) {
				$tags[] = array(
					"id" => $tag->id(),
					"name" => $tag->name()
				);
			}
			$wcThemeAssigns["tags"] = $tags;

			break;
		case "ALSO":
			$wcThemeAssigns["mode"] = $mode;
			$wcThemeAssigns["mode-description"] = $mode;

			$wcThemeAssigns["target"] = $wcRuleAction->target;

			$also = array();
			foreach($wcRuleAction->tagsAlso() as $tag) {
				$also[] = array(
					"id" => $tag->id(),
					"name" => $tag->name()
				);
			}
			$wcThemeAssigns["also"] = $also;

			break;
		case "IF":
			$wcThemeAssigns["mode"] = $mode;
			$wcThemeAssigns["mode-description"] = $mode;

			$wcThemeAssigns["target"] = $wcRuleAction->target;

			$also = array();
			foreach($wcRuleAction->tagsAlso() as $tag) {
				$also[] = array(
					"id" => $tag->id(),
					"name" => $tag->name()
				);
			}
			$wcThemeAssigns["also"] = $also;

			break;
		default:
			$wcThemeAssigns["mode-description"] = $mode;
			unset($wcThemeAssigns["mode-template"]);
			break;
	}
} else {
	if(!$wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
		$wcThemeAssigns["error-message"] = "You don't have enough priviliges";
		require "{$this->_themeRoot}/controlers/contents/c.403.php";
	} else {
		$wcThemeAssigns["error-message"] = "There seems to be a problem with parameters";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
}
?>